#include "Character.h"






void Character::Link(CollisionSolver *solver)
{
	p_solver=solver;
}

void Character::Collision(float dt)
{
	if (m_velocity.x!=0 || m_velocity.y!=0)
	{
		if (Supported())
		{
			//check for floor underneath us
			TILETYPE tt=Floorcheck();
			if (tt==TT_empty)
			{
				m_state=CS_fall;
				p_plat=NULL;
			}
		}
		//only do collision tests if we're moving
		if (p_solver->getCollision(&m_position,&m_velocity,dt,sf::Vector2f (32,64)))
		{
			m_state=CS_stand;
			Land();
			p_plat=p_solver->getElement(sf::Vector2f(m_position.x+16,m_position.y+34));
			if (p_plat!=NULL)
			{
				m_velocity=m_velocity+p_plat->getVelocity();
			}
		}
		ProcessCollision(p_solver->getActorCollision(this,&m_velocity,dt));
	}
}
void Character::ProcessCollision(Actor *actor)
{

}
void Character::Land()
{

}

void Character::Gravity(float dt)
{
	if (!Supported())
	{
		if (m_velocity.y<18)
		{
			m_velocity.y+=2*dt;
		}
	}
	if (m_state==CS_jump && m_velocity.y>=0)
	{
		m_state=CS_fall;
		
	}
}

TILETYPE Character::Floorcheck()
{
	TILETYPE p0=p_solver->getTileType(sf::Vector2f(m_position.x+16,m_position.y+34));
	TILETYPE p1=p_solver->getTileType(sf::Vector2f(m_position.x-16,m_position.y+34));	
	
	if (p0==TT_plat || p1==TT_plat)
	{
		return TT_plat;
	}
	if (p0==TT_solid||p1==TT_solid)
	{
		return TT_solid;
	}
	if (p_plat!=NULL)
	{
		int x0=p_plat->getPosition().x-(p_plat->getTexture()->getSize().x/2)-16;
		int x1=p_plat->getPosition().x+(p_plat->getTexture()->getSize().x/2)+16;
		if (m_position.x>x0 && m_position.x<x1)
		{
			return TT_plat;
		}
	}
	return TT_empty;
}

void Character::Friction(float dt)
{
	if (m_state==CS_stand||m_state==CS_move)
	{
			if (p_plat==NULL)
				{
					if (m_velocity.x>-2 && m_velocity.x<2)
					{
						m_velocity.x=0;
						m_state=CS_stand;
					}
					if (m_velocity.x>0)
					{
						m_velocity.x-=2*dt;
						if (m_velocity.x<0)
						{
							m_velocity.x=0;
						}
					}
					else if (m_velocity.x<0)
					{
						m_velocity.x+=2*dt;
						if (m_velocity.x>0)
						{
							m_velocity.x=0;
						}
					}
				}
				else
				{
					if (m_velocity.x>p_plat->getVelocity().x-2 && m_velocity.x<p_plat->getVelocity().x+2)
					{
						m_velocity.x=p_plat->getVelocity().x;
					}
					if (m_velocity.x>p_plat->getVelocity().x)
					{
						m_velocity.x-=2*dt;
						if (m_velocity.x<p_plat->getVelocity().x)
						{
							m_velocity.x=p_plat->getVelocity().x;
						}
					}
					else if (m_velocity.x<p_plat->getVelocity().x)
					{
						m_velocity.x+=2*dt;
						if (m_velocity.x>p_plat->getVelocity().x)
						{
							m_velocity.x=p_plat->getVelocity().x;
						}
					}

				}

	}
}

bool Character::Supported()
{
	if (m_state==CS_fall||m_state==CS_jump)
	{
		return false;
	}
	return true;
}

CSTATE Character::getState()
{
	return m_state;
}
bool Character::getFlipped()
{
	return m_flip;
}

sf::Texture *Character::getMiniview()
{
	return m_miniview;
}

bool Character::Vore(bool flip)
{
	return false;
}

bool Character::NotShlurped()
{
	if (m_state==CS_stealthcv||m_state==CS_defeatcv)
	{
		return false;
	}
	return true;
}

bool Character::NotBusy()
{
	if (m_state==CS_lowattack||m_state==CS_highattack||m_state==CS_specialattack||!NotShlurped())
	{
		return false;
	}
	return true;
}